/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ac.lib.data;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author empcl_000
 */
public class dateTime {

    static String FormatDetaToDB = "yyyy-MM-dd";
    static String FormatDetaToView = "dd/MM/yyyy";
    static String FormatTimeToView = "HH:mm.ss";

    public static Date addDays(Date mydate, int days) {

        mydate.setTime(mydate.getTime() + days * 24 * 60 * 60 * 1000);

        return mydate;
    }

    public static DateFormat returnDateFrom(String Status) {

        String f = FormatDetaToView;

        if (Status.equalsIgnoreCase("todb")) {
            f = FormatDetaToDB;
        }
        final DateFormat df = new SimpleDateFormat(f);
        return df;

    }

    public static String updateSerechDates(String field, String date1, String date2) {
        String r = "";
        try {
            String d1 = updateDateFormat("todb", date1);
            String d2 = updateDateFormat("todb", date2);
            r = "`" + field + "`" + " BETWEEN '" + d1 + "' AND  '" + d2 + "'";

        } catch (ParseException ex) {
            Logger.getLogger(dateTime.class.getName()).log(Level.SEVERE, null, ex);
        }
        return r;
    }

    public static String updateSerechDates(String field, Date date1, Date date2) {
        String r = "";
        try {
            String d1 = updateDateFormat("todb", date1);
            String d2 = updateDateFormat("todb", date2);
            r = "" + field + "" + " BETWEEN '" + d1 + "' AND  '" + d2 + "'";

        } catch (ParseException ex) {
            Logger.getLogger(dateTime.class.getName()).log(Level.SEVERE, null, ex);
        }
        return r;
    }

    public static String updateSerechDate(String field, Date date1) {
        String r = "";
        try {
            String d1 = updateDateFormat("todb", date1);

            r = "`" + field + "`" + " = '" + d1 + "'";

        } catch (ParseException ex) {
            Logger.getLogger(dateTime.class.getName()).log(Level.SEVERE, null, ex);
        }
        return r;
    }

    public static String updateSerechLessDate(String field, String op, Date date1) {
        String r = "";
        try {
            String d1 = updateDateFormat("todb", date1);

            r = "" + field + " " + op + "  '" + d1 + "'";

        } catch (ParseException ex) {
            Logger.getLogger(dateTime.class.getName()).log(Level.SEVERE, null, ex);
        }
        return r;
    }

    public static String updateDateFormat(String Status, Date inputDate) throws ParseException {

        String f = FormatDetaToView;
        if (Status.equalsIgnoreCase("todb")) {
            f = FormatDetaToDB;
        }

        DateFormat df = new SimpleDateFormat(f);

        return df.format(inputDate);

    }

    public static String updateDateFormat(String Status, String inputDate) throws ParseException {

        String f = FormatDetaToView;
        if (Status.equalsIgnoreCase("todb")) {
            f = FormatDetaToDB;
        }

        DateFormat df = new SimpleDateFormat(f);
        Date otuputDate = df.parse(inputDate);

        return otuputDate.toString();

    }

    public static Date convertStringFromDBTodate(String Date) throws ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat(FormatDetaToDB);

        java.util.Date temp = sdf.parse(Date);

        return temp;

    }

    public static String convertdateToString(String Status, Date myDate) throws ParseException {

        String f = FormatDetaToView;

        if (Status.equalsIgnoreCase("todb")) {
            f = FormatDetaToDB;
        }

        String temp = new SimpleDateFormat(f).format(myDate);

        return temp;
    }

    public static String now(String Status) {

        String f = "";
        if (Status.equalsIgnoreCase("date") || Status.equalsIgnoreCase("all")) {

            f += " " + FormatDetaToView;
        }
        if (Status.equalsIgnoreCase("time") || Status.equalsIgnoreCase("all")) {
            f += " " + FormatTimeToView;
        }

        final DateFormat df = new SimpleDateFormat(f);
        return df.format(new Date());

    }

    public static String now(String Status, String Type) {

        String f = FormatDetaToView;
        if (Status.equalsIgnoreCase("date") || Status.equalsIgnoreCase("all")) {

            if (Type.equalsIgnoreCase("todb")) {
                f = FormatDetaToDB;
            }

        }

        final DateFormat df = new SimpleDateFormat(f);
        return df.format(new Date());

    }
}
